package MazeSolver;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.UIManager;

public class MazeView {
	private JFrame window;
	private MapPanel mapPanel;
	
	private MazeModel model;
	
	private MazeMouseController mouseController;
	private JButton solveButton;
	private MazeButtonController buttonController;

	public MazeView(String[] args) {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch(Exception e) {
			//silently ignore
		}

		window = new JFrame("MazeSolver 2.0");
		window.setSize(900, 500);
		window.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		window.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				window.dispose();
				System.exit(0);
			}
		});
		
		mapPanel = new MapPanel(args[0]);
		
		mapPanel.setLayout(new BoxLayout(mapPanel, BoxLayout.PAGE_AXIS));
		mapPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
		
		solveButton = new JButton("Solve");
		
		Container contentPane = window.getContentPane();
		contentPane.add(mapPanel,BorderLayout.CENTER);
		contentPane.add(solveButton,BorderLayout.SOUTH);
		
		model = new EuclideanModel(mapPanel.getMaze(),this);
		
		mouseController = new MazeMouseController(this,model);
		buttonController = new MazeButtonController(this,model);
		
		mapPanel.addMouseListener(mouseController);
		solveButton.addActionListener(buttonController);
		
		window.setVisible(true);
		
		//model.solve();
	}
	
	JButton getSolveButton() {
		return solveButton;
	}

	public MapPanel getMapPanel() {
		return mapPanel;
	}

	public void setMapPanel(MapPanel mapPanel) {
		this.mapPanel = mapPanel;
	}

	public void updateOverlay(int x, int y, Color c) {
		mapPanel.setOverlayColor(x, y, c);
	}

	public void printSolution(int x, int y, Color c) {
		mapPanel.setOverlaySolidColor(x, y, c);
	}

	public void drawMaze() {
		mapPanel.paintComponent(mapPanel.getGraphics());
	}

	public void updateStart() {
		mapPanel.setStart(model.getStart());
		mapPanel.paintComponent(mapPanel.getGraphics());
	}
	
	public void updateEnd() {
		mapPanel.setEnd(model.getEnd());
		mapPanel.paintComponent(mapPanel.getGraphics());
	}

	public boolean imageContainsClick(MouseEvent me) {
		if(me.getX() < mapPanel.getWidth()/2 - mapPanel.getMaze().getWidth()/2)
			return false;
		if(me.getX() > mapPanel.getWidth()/2 + mapPanel.getMaze().getWidth()/2)
			return false;
		if(me.getY() < mapPanel.getHeight()/2 - mapPanel.getMaze().getHeight()/2)
			return false;
		if(me.getY() > mapPanel.getHeight()/2 + mapPanel.getMaze().getHeight()/2)
			return false;
		
		return true;
	}

	public int getHorizontalOffset() {
		return mapPanel.getWidth()/2 - mapPanel.getMaze().getWidth()/2;
	}
	
	public int getVerticalOffset() {
		return mapPanel.getHeight()/2 - mapPanel.getMaze().getHeight()/2;
	}

	public void resetMaze() {
		mapPanel.reset();
	}

}
